package com.le.buddha;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guole
 * @created 2024/10/4
 */
public class GuoleCosyVoiceB {
    private List<byte[]> speechByte = new ArrayList<>();

    public void addByte(ByteBuffer message) {
        System.out.println("长度：" + message.remaining());
        if (message.remaining() > 0) {
            byte[] bytesArray = new byte[message.remaining()];
            message.get(bytesArray, 0, bytesArray.length);
            speechByte.add(bytesArray);

        }
        System.out.println(speechByte);
    }

    public byte[] getSpeechByte() {
         return mergeArrays(speechByte);
    }

    private byte[] mergeArrays(List<byte[]> arrays) {
        int length = 0;
        int byteLength = 0;
        for (byte[] array : arrays) {
            byteLength += array.length;
        }
        byte[] result = new byte[byteLength];


        for (int i = 0; i < arrays.size(); i++) {
            System.arraycopy(arrays.get(i), 0, result, length, arrays.get(i).length);
            length+=arrays.get(i).length;
        }


        return result;
    }


}
